﻿package com.bizmlib.ui.scrolling
{
	import flash.display.MovieClip;
	import com.bizmlib.ui.scrolling.ScrollerEvent;
	
	
	public class AbstractScroller extends MovieClip
	{
		protected static const STATE_INIT:Number = 0;
		protected static const STATE_RUNNING:Number = 1;
	
		protected var WINDOW_INITIAL:Number = 1;
		protected var POSITION_INITIAL:Number = 0;
		//protected var ENABLED_INITIAL:Boolean = true;
	
		protected var state:Number;
		protected var __window:Number;
		protected var __position:Number = 0;
		protected var positionOldValue:Number;
	
	
		public function AbstractScroller()
		{
			state = STATE_INIT;
	
			init();
			createChildren();
			completeInit();
			
			state = STATE_RUNNING;
		}
	
	
		public function set window(value:Number):void
		{
			//trace(this + ".window (set) " + arguments);
			setWindowValue(value);
			updateWindow();
			updatePosition();
		}
		public function get window():Number
		{
			return __window;
		}
		protected function setWindowValue(value:Number):void
		{
			//trace(this + ".setWindowValue(" + arguments + ")");
			__window = Math.max(0, Math.min(1, value));
		}
		public function set position(value:Number):void
		{
			//trace(this + ".position (set) " + arguments);
			positionOldValue = position;
			setPositionValue(value);
			updatePosition();
			positionOldValue = -1;
		}
		public function get position():Number
		{
			return __position;
		}
		protected function setPositionValue(value:Number):void
		{
			//trace(this + ".setPositionValue(" + arguments + ")");
			__position = Math.max(0, Math.min(1, value));
			//__position = value;
		}
		override public function set enabled(value:Boolean):void
		{
			setEnabledValue(value);
			updateAccessibility();
		}
		override public function get enabled():Boolean
		{
			return super.enabled;
		}
		protected function setEnabledValue(value:Boolean):void
		{
			super.enabled = value;
		}
		
		
		public function clean():void
		{
		}
		
		
		protected function init():void
		{
			positionOldValue = -1;
		}
		protected function createChildren():void
		{
		}
		protected function completeInit():void
		{
			window = isNaN(window)?WINDOW_INITIAL:window;
			position = isNaN(position) ? POSITION_INITIAL : position;
			enabled = enabled;
		}
		protected function updateWindow():void
		{
		}
		protected function updatePosition():void
		{
		}
		protected function updateAccessibility():void
		{
		}
		protected function scrollBy(scrollValue:Number):void
		{
			//trace(this + ".scrollBy(" + arguments + ")");
			scrollTo(position + scrollValue);
		}
		protected function scrollTo(scrollValue:Number, doNotUpdatePosition:Boolean = false):void
		{
			//trace(this + ".scrollTo(" + arguments + ")");
			doNotUpdatePosition = doNotUpdatePosition || false;
	
			setPositionValue(scrollValue);
			if(!doNotUpdatePosition)
			{
				updatePosition();
			}
			
			//dispatchEvent({type:EVENT_SCROLL, target:this, position:position});
			dispatchEvent(new ScrollerEvent(ScrollerEvent.SCROLL, position));
		}
	
		override public function toString():String
		{
			return "[com.bizmlib.ui.scrolling.AbstractScroller]";
		}
	}
}